package com.zdy.worldtion.ui;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.zdy.worldtion.R;

public class LoginActivity extends AppCompatActivity {

    private EditText etPhone; // 手机号输入框
    private EditText etCode;  // 验证码输入框
    private TextView tvGetYzm; // 获取验证码按钮
    private TextView tvLogin;  // 登录按钮
    private TextView tvFindPwd; // 立即注册按钮

    private CountDownTimer countDownTimer; // 倒计时器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login); // 关联XML布局

        initView();
        initEvent();
    }

    // 初始化控件
    private void initView() {
        etPhone = findViewById(R.id.et_phone);
        etCode = findViewById(R.id.et_phone); // 注意XML中验证码输入框需补充id: et_code
        tvGetYzm = findViewById(R.id.tv_get_yzm);
        tvLogin = findViewById(R.id.tv_login);
        tvFindPwd = findViewById(R.id.tv_find_pwd);
    }

    // 初始化事件监听
    private void initEvent() {
        // 获取验证码点击事件
        tvGetYzm.setOnClickListener(v -> getVerificationCode());

        // 登录按钮点击事件
        tvLogin.setOnClickListener(v -> login());

        // 立即注册点击事件
        tvFindPwd.setOnClickListener(v -> {
            // 跳转到注册页面逻辑
            Toast.makeText(LoginActivity.this, "跳转到注册页面", Toast.LENGTH_SHORT).show();
        });
    }

    // 获取验证码逻辑
    private void getVerificationCode() {
        String phone = etPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (phone.length() != 11) {
            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return;
        }

        // 模拟获取验证码（实际项目中需调用接口）
        Toast.makeText(this, "验证码已发送", Toast.LENGTH_SHORT).show();

        // 启动倒计时
        startCountDown();
    }

    // 倒计时逻辑
    private void startCountDown() {
        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvGetYzm.setEnabled(false);
                tvGetYzm.setText(millisUntilFinished / 1000 + "s后重新获取");
            }

            @Override
            public void onFinish() {
                tvGetYzm.setEnabled(true);
                tvGetYzm.setText("获取验证码");
            }
        }.start();
    }

    // 登录逻辑
    private void login() {
        String phone = etPhone.getText().toString().trim();
        String code = etCode.getText().toString().trim();

        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (phone.length() != 11) {
            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(code)) {
            Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (code.length() != 6) {
            Toast.makeText(this, "请输入6位验证码", Toast.LENGTH_SHORT).show();
            return;
        }

        // 模拟登录成功（实际项目中需调用登录接口）
        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
        // 登录成功后可跳转到首页
        // finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 销毁时取消倒计时，避免内存泄漏
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }
}